package com.elite;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.tempuri.WebServiceTest;
import org.tempuri.WmgwLocator;



public class ZHJSMS implements Runnable{
	private static final Log log=LogFactory.getLog(ZHJSMS.class);
	public static com.elite.FtpBean ftpbean;
	public static String times;
	private boolean flag = true; 
	private static int Hz =1; 
	private static String sql =" select * from Documentout where DOCTYPE=3 and ((Status=0) or (Status!=1 and TrialCount<TrialCountLimit)) "; 
	private static String update_sql="";
	public static String username;
	public static String username1;
	public static String password;
	public static String password1;
	public static String XHH;
	public static String XHH1;
	private static String LastTryDateTime="";
	private static String FinishDateTime="";
	public static String IP;
	private static int xh_int=0;
	private static SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
	
	
    private static String get_xh(int a){
    	if(a>=0&&a<10){
    		return "000"+a;
    	}else if(a>=10&&a<=99){
    		return "00"+a;
    	}else if(a>=100&&a<999){
    		return "0"+a;
    	}else{
    		return ""+a;
    	}
    }
	public static String getIP() {
		return IP;
	}
	public static void setIP(String ip) {
		IP = ip;
	}
	public static com.elite.FtpBean getFtpbean() {
		return ftpbean;
	}
	public static void setFtpbean(com.elite.FtpBean ftpbean) {
		ZHJSMS.ftpbean = ftpbean;
	}
	public static String getTimes() {
		return times;
	}
	public static void setTimes(String times) {
		ZHJSMS.times = times;
	}
	public static String getUsername() {
		return username;
	}
	public static void setUsername(String username) {
		ZHJSMS.username = username;
	}
	public static String getUsername1() {
		return username1;
	}
	public static void setUsername1(String username1) {
		ZHJSMS.username1 = username1;
	}
	public static String getPassword() {
		return password;
	}
	public static void setPassword(String password) {
		ZHJSMS.password = password;
	}
	public static String getPassword1() {
		return password1;
	}
	public static void setPassword1(String password1) {
		ZHJSMS.password1 = password1;
	}
	public static String getXHH() {
		return XHH;
	}
	public static void setXHH(String xhh) {
		XHH = xhh;
	}
	public static String getXHH1() {
		return XHH1;
	}
	public static void setXHH1(String xhh1) {
		XHH1 = xhh1;
	}
	public static String getdesc(String a){
    	if(a.equals("-1")){
    		return "参数为空。信息、电话号码等有空指针，登陆失败";
    	}else if(a.equals("-2")){
    		return "电话号码个数超过100";
    	}else if(a.equals("-11")){
    		return "电话号码中有非数字字符";
    	}else if(a.equals("-12")){
    		return "有异常电话号码";
    	}else if(a.equals("-13")){
    		return "电话号码个数与实际个数不相等";
    	}else if(a.equals("-14")){
    		return "实际号码个数超过100";
    	}else if(a.equals("-101")){
    		return "发送消息等待超时";
    	}else if(a.equals("-102")){
    		return "发送或接收消息失败";
    	}else if(a.equals("-103")){
    		return "接收消息超时";
    	}else if(a.equals("-200")){
    		return "其他错误";
    	}else if(a.equals("-999")){
    		return "web服务器内部错误";
    	}else{
    		return "未知错误:"+a;
    	}
    }
	public void run(){
		log.info("---------------------中航技短信接口----------------------------");
		WmgwLocator.wmgwSoap_address=IP;	
		while(flag){
			try{
				log.info("sss:"+times);
				Hz=Integer.parseInt(times);
			}catch(Exception e){
				log.info("错误:"+e);
				log.info("因为获取运行频率出错，改为默认频率1分钟运行");
			}
			log.info("扫描documentout表中未处理的记录："+sql);
			List list=null;
			try{
				list=ftpbean.chaxun(sql);
			}catch(Exception e){
				log.info("获取扫描结果错误："+e);
			}
			if(list!=null){
				for(int i=0;i<list.size();i++){
					Map map=(Map)list.get(i); 
					String StringFrield2=Util.filterForObject(map.get("StringFrield2"));
					String mobile=Util.filterForObject(map.get("DocumentTo"));
					String DocumentPath=Util.filterForObject(map.get("DocumentPath"));
					String TrialCountLimit=Util.filterForObject(map.get("TrialCountLimit"));
					String DocumentOut_GUID=Util.filterForObject(map.get("DocumentOut_GUID"));
					if(1==1){
						if(1==1){
							Date d = new Date();
							LastTryDateTime = formatter.format(d);
							FinishDateTime = formatter.format(d);
							long longtime = d.getTime();
							String longtime1=longtime+"";
							longtime1=XHH+longtime1.substring(longtime1.length()-5, longtime1.length());
							String longtime2=XHH1+longtime1.substring(longtime1.length()-5, longtime1.length());
                            String line="";
                            
//                            String xh=longtime1.substring(longtime1.length()-4, longtime1.length());
                            if(xh_int>9999){
                            	xh_int=0;
                            }
                            String xh=get_xh(xh_int);
                            log.info("xh:"+xh);
                            xh_int=xh_int+1;
                            line=WebServiceTest.sendSms(username1,password1,mobile,DocumentPath,xh);
							if(line!=null&&line!=""){
								log.info("返回结果:"+line);
								if(line.length()>10&&line.length()<25){
									update_sql=" update DocumentOut set stringfield2='"+xh+"',Status=1,TrialCount=TrialCount+1,LastTryDateTime=to_date('"+LastTryDateTime+"','yyyy-mm-dd hh24:mi:ss'),FinishDateTime=to_date('"+FinishDateTime+"','yyyy-mm-dd hh24:mi:ss'),ModifiedDate=sysdate where DocumentOut_GUID='"+DocumentOut_GUID+"'";
									
								}else{
									update_sql=" update DocumentOut set FailCode="+line+",FailDescription='"+getdesc(line)+"',stringfield2='"+xh+"',Status=5,TrialCount=TrialCount+1,LastTryDateTime=to_date('"+LastTryDateTime+"','yyyy-mm-dd hh24:mi:ss'),FinishDateTime=to_date('"+FinishDateTime+"','yyyy-mm-dd hh24:mi:ss'),ModifiedDate=sysdate where DocumentOut_GUID='"+DocumentOut_GUID+"'";
								}
								log.info("update_sql:"+update_sql);
								try{
									int a=ftpbean.update(update_sql);
									log.info("是否更新DocumentOut成功:"+a);
								}catch(Exception e){
									log.info("更新DocumentOut出错:"+e);
								}
								
							}else{
								log.info("DocumentOut查询无结果");
							}

							
						}else{
							log.info("手机号"+mobile+"StringFrield2字段错误");
						}
						
					}else{
						log.info("手机号"+mobile+"StringFrield2字段错误");
					}
				}
				
			}else{
				log.info("没有需要发送的短信");
			}
                
			try { 
				Thread.sleep(Hz*1000); 
				} 
				catch (Exception e) {
					log.info("定时错误:"+e);
					} 
		}
	}

	

}





















